package com.sky.controller.admin;

import com.sky.config.OssConfig;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api("通用模块")
@Slf4j
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {

        log.info("img={}", file.getOriginalFilename());

        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名  XXX.png  ==> png
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //UUID 获取唯一名称,防止文件覆盖(使用 日期创建文件夹更好，可以防止文件过多引起读取缓慢)
        String newName = UUID.randomUUID().toString() + suffix;

        String oosUrl = aliOssUtil.upload(file.getBytes(), newName);

        return Result.success(oosUrl);

    }

}
